// import formJs from "./regexDetail.js"; //新增弹框配置项
// import TableData from "@/components/public/el-table"; //主页面列表
// import TableLyout from '@/components/commonPublic/tableLyout.vue'

import {
    getBaseDepartmentUser, //查人员
    getUserCompanyTreeList
} from "@/api/request.js";

export default {
    props: [
        "dialogVisible",
        "title",
        "comonId",
        "selectArr","isMultiple"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            tableData: [],
            isLock: false,
            dialogHeight: null,
            btnShow: true,
            WbsRow: null,

            companyInfo: [], //公司树信息
            highlightBd: true,
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            departmentList: [], //部门
            num: null,
            personList: [], //人员列表新增
            checkboxGroup1: [],
            boys: require("@/assets/img/organization/boys.png"),
            girls: require("@/assets/img/organization/girls.png"),
            newData: [],
            treeText: {
                companyId: null,
                userId: null,
                text: null,
            },
            propText: null,
            userInfo:{},
            selectPerson:[],
            allUserInfo: [],

        };
    },
    components: {
        // TableLyout,
        // TableData,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                
                if (newVal) {
                    
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        comonId: {
            handler(newVal) {
                if (newVal) {
                    this.getBaseCompanyList(newVal)
                }
            },
            immediate: true,
        },
        selectArr: {
            handler(newVal) {
                if (newVal) {
                    this.selectPerson=newVal;
                    
                }
            },
            immediate: true,
        },

        // this.newData.map((item) => {
        //     this.checkboxGroup1.push(item.fUserid);
        //   });
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        closeDialogPerson() {
            this.$emit("closePerson");
        },
        // 点击材料
        rowChange(val) {
            this.WbsRow = val
            // this.dataFid = val.fId;
        },
        // 树选人
        // 树结构
        getBaseCompanyList(id) {
            this.treeText = {
                companyId: id,
                userId: this.userInfo.userId,
                text: "1", // 0其他  1 砼拌合站 2水稳 3沥青
            };

            this.personList = [];
            this.departmentList = [];
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    this.loadingEditDialogTree = false;
                    if (res.errcode == 0) {
                        this.companyTreeData = res.data.userCompanyTreeList;
                        this.companyInfo = this.tranListToTreeData(
                            res.data.userCompanyTreeList
                        );
                    } else {
                        this.$message.error("获取组织机构单位", res.msg);
                    }
                })
                .catch((error) => {
                    console.log("获取组织机构单位,接口报错", error);
                });
        },
        tranListToTreeData(data) {
            let result = [];
            if (!Array.isArray(data)) {
                return result;
            }
            // 清除已存在的数据  重新赋值
            data.forEach((item) => {
                delete item.children;
            });

            let map = {};
            data.forEach((item) => {
                map[item.fCompanyid] = item;
            });

            data.forEach((item) => {
                let parent = map[item.fParentid];
                if (parent) {
                    (parent.children || (parent.children = [])).push(item);
                } else {
                    result.push(item);
                }
            });
            return result;
        },
        //根据树  调部门
        selectCompany(e) {
            this.getBaseDepartmentUser(e.fCompanyid);
        },
        // 部门管理-查询部门列表以及下属人员
        getBaseDepartmentUser(id) {
            getBaseDepartmentUser({
                    fCompanyid: id
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.departmentList = res.data.childList;
                    } else {
                        this.$message.error("部门管理-查询部门列表以及下属人员", res.msg);
                    }
                })
                .catch((error) => {
                    console.log("部门管理-查询部门列表以及下属人员,接口报错", error);
                });
        },
        // 选择部门
        seletDepartment(e) {
            
            if(this.isMultiple){
                
                this.newData=this.selectPerson
                this.selectPerson.map(item=>{
                  this.checkboxGroup1.push(item.fUserid);
                })
            }else {
             
                    this.newData=this.selectPerson
                    this.checkboxGroup1=this.selectPerson.fUserid;

                
            }
            this.num = e.fDepartmentId;
            this.personList = [];
            let obj = {};
            // let reduce = [];
            this.personList = e.personList.reduce(function (item, next) {
                //item为没有重复id的数组，next为当前对象
                obj[next.fUserid] ? "" : (obj[next.fUserid] = true && item.push(next));
                return item;
            }, []);
        },
        selectRealname(e, data) {
            if(this.isMultiple){
                this.allUserInfo = this.allUserInfo.concat(data);
                let obj = {};
                let reduce = [];
                reduce = this.allUserInfo.reduce((item, next) => {
                  //item为没有重复id的数组，next为当前对象
                  obj[next.fUserid] ? "" : (obj[next.fUserid] = true && item.push(next));
                  return item;
                }, []);
                this.newData = [];
                reduce.map((item) => {
                  this.checkboxGroup1.map((i) => {
                    if (item.fUserid == i) {
                      this.newData.push(item);
                    }
                  });
                });
            }else {
                data.map((item) => {
                    if (e == item.fUserid) {
                        this.newData = item;
                    }
                });
       
            }
           
        },
        // 保存
        submitMember() {
            if(this.newData!=null){
                this.$emit('getperson', this.newData)
                this.visible = false;
            }else {
                this.$message.warning("请选择");
            }
           
            
        },
        // 树选人


    },
};